package com.hxd.otc.module.my.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseAdapter;
import com.hxd.otc.common.enums.BankType;
import com.hxd.otc.module.my.bean.BankCardBean;
import com.hxd.otc.utils.StringUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lzy on 2018/6/7 0007.
 * description ${银行卡列表}
 * <p>
 */
public class BankCardAdapter extends BaseAdapter<BankCardAdapter.BankCardHolder, BankCardBean> {

    public BankCardAdapter(Context context) {
        super(context);
    }

    @Override
    protected void onBindHolder(@NonNull BankCardHolder holder, final int position) {
        final BankCardBean item = getItem(position);

        BankType bankType = BankType.getBankTypeByValue(item.getType());
        holder.ivBankcardLogo.setImageResource(bankType.getBankCardLogo());
        holder.tvBankcardNumber.setText(String.format("%s  %s", StringUtil.emptyIfNull(item.getRealName()), encryptPayNo(item.getPayNo())));

        switch (item.getType()) {//1微信2支付宝3银行卡
            case 1:
                holder.tvBankcardName.setText(R.string.weixin);
                break;
            case 2:
                holder.tvBankcardName.setText(R.string.alipay);
                break;
            case 3:
            default:
                holder.tvBankcardName.setText(StringUtil.emptyIfNull(item.getBankName()));
                break;
        }
        holder.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnDeleteBankListener==null)return;
                mOnDeleteBankListener.onDelete(position,item);
            }
        });
    }

    @NonNull
    private String encryptPayNo(String payNo) {
        payNo = StringUtil.emptyIfNull(payNo);
        if (payNo.length() <= 6) return payNo;
        return String.format("%s****%s", payNo.substring(0, 2), payNo.substring(payNo.length() - 4));
    }

    @NonNull
    @Override
    public BankCardHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new BankCardHolder(mInflater.inflate(R.layout.item_bank_card, parent, false));
    }

    static class BankCardHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.cl_item)
        RelativeLayout clItem;
        @BindView(R.id.iv_bankcard_logo)
        ImageView ivBankcardLogo;
        @BindView(R.id.tv_bankcard_name)
        TextView tvBankcardName;
        @BindView(R.id.iv_delete)
        ImageView ivDelete;
        @BindView(R.id.tv_bankcard_number)
        TextView tvBankcardNumber;

        public BankCardHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private OnDeleteBankListener mOnDeleteBankListener;

    public void setOnDeleteBankListener(OnDeleteBankListener onDeleteBankListener) {
        this.mOnDeleteBankListener = onDeleteBankListener;
    }

    public interface OnDeleteBankListener{
        void onDelete(int position,BankCardBean item);
    }
}
